import json
import time
import os, json
from datetime import datetime

from xlrd import xldate_as_tuple

from Base import GetYamlFile
from Base.ReadExcel import Read_Excel
from Base.WriteExcel import Write_excel


def BugStatistics(Year, Mon):
    '''通过yaml文件获取xlsx路径'''
    YmlList = GetYamlFile.getYamlFile()
    BugStatisticsPath = YmlList["BugStatisticsPath"]
    PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, BugStatisticsPath)
    readBook = Read_Excel(REPORT_XLSX_DIR)
    num = readBook.get_RowNums("bug详情")
    # print(num)
    # 初始化Total，Priority所需要的值都为0
    Total = 0
    Blocker = 0
    Critical = 0
    Major = 0
    Minor = 0
    # 通过for循环语句读取所有的creattime及Priority状态
    for item in range(1, num):
        CreatTime = readBook.read_Excel("bug详情", item, 9)
        Priority = readBook.read_Excel("bug详情", item, 5)
        # 获取CreatTime未float类型 转换为datetime类型
        CreatTime = datetime(*xldate_as_tuple(CreatTime, 0))
        # 获取CreatYear 创建年
        CreatYear = CreatTime.year
        # 获取CreatMon 创建月
        CreatMon = CreatTime.month
        # 判断读取的CreatYear及CreatMon是否与入参Year，Mon相等
        if CreatYear == Year and CreatMon == Mon:
            Total += 1
            if Priority == "Blocker":
                Blocker += 1
            elif Priority == "Critical":
                Critical += 1
            elif Priority == "Major":
                Major += 1
            elif Priority == "Minor":
                Minor += 1

        else:
            continue
    # 获取得知CreatYear及CreatMon下所获取的TotalTotal,Blocker,Critical,Major,Minor所需要的值 并且返回结果
    return Total, Blocker, Critical, Major, Minor


def setData():
    '''通过yaml文件获取xlsx路径'''
    list = []
    YmlList = GetYamlFile.getYamlFile()
    BugStatisticsPath = YmlList["BugStatisticsPath"]
    PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, BugStatisticsPath)
    readBook = Read_Excel(REPORT_XLSX_DIR)
    num = readBook.get_RowNums("bug详情")
    for item in range(1, num):
        CreatTime = readBook.read_Excel("bug详情", item, 9)
        Priority = readBook.read_Excel("bug详情", item, 5)
        # 获取CreatTime未float类型 转换为datetime类型
        CreatTime = datetime(*xldate_as_tuple(CreatTime, 0))
        # 获取CreatYear 创建年
        CreatYear = CreatTime.year
        # 获取CreatMon 创建月
        CreatMon = CreatTime.month
        result = {
            "Priority": Priority,
            "CreatYear": CreatYear,
            "CreatMon": CreatMon
        }
        list.append(result)
    return list


def getData():
    list = []
    YmlList = GetYamlFile.getYamlFile()
    BugStatisticsPath = YmlList["BugStatisticsPath"]
    PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, BugStatisticsPath)
    readBook = Read_Excel(REPORT_XLSX_DIR)
    num = readBook.get_RowNums("bug统计")
    # 通过for循环获取工作簿中所有的统计时间的年：CreatYear  以及 月：CreatMon
    for item in range(1, num):
        CreatTime = readBook.read_Excel("bug统计", item, 0)
        CreatTime = datetime(*xldate_as_tuple(CreatTime, 0))
        CreatYear = CreatTime.year
        CreatMon = CreatTime.month
        Time = {
            "CreatYear": CreatYear,
            "CreatMon": CreatMon
        }
        list.append(Time)
    # 数组输出
    return list


def addDate(Year, Mon, list):
    YmlList = GetYamlFile.getYamlFile()
    BugStatisticsPath = YmlList["BugStatisticsPath"]
    PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, BugStatisticsPath)
    readBook = Read_Excel(REPORT_XLSX_DIR)
    num = readBook.get_RowNums("bug统计")
    wt = Write_excel(REPORT_XLSX_DIR, "bug统计")
    for item in range(1, num):
        CreatTime = readBook.read_Excel("bug统计", item, 0)
        CreatTime = datetime(*xldate_as_tuple(CreatTime, 0))
        CreatYear = CreatTime.year
        CreatMon = CreatTime.month
        # 通过对比
        if Year == CreatYear and Mon == CreatMon:
            wt.write(item + 1, 2, list[0])
            wt.write(item + 1, 3, list[1])
            wt.write(item + 1, 4, list[2])
            wt.write(item + 1, 5, list[3])
            wt.write(item + 1, 6, list[4])


if __name__ == '__main__':
    # list = getData()
    # print(list)
    # Data = setData()
    # for item in list:
    #     result = BugStatistics(item["CreatYear"], item["CreatMon"])
    #     print(result)
    #     addDate(item["CreatYear"], item["CreatMon"], result)
    number ={
        "get": "requests.get(self.url, params=self.data, headers=self.headers)",
        "post": "requests.post(self.url, params=self.data, headers=self.headers)",
        "Get": "requests.get(self.url, params=self.data, headers=self.headers)",
        "Post": "requests.post(self.url, params=self.data, headers=self.headers)",
    }.get("Post",4)
    print(number)
